package pers.dawnyang.modular.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import pers.dawnyang.common.core.domain.entity.SysDictType;
import pers.dawnyang.common.core.domain.entity.SysDictValue;
import pers.dawnyang.config.cache.DictCacheService;
import pers.dawnyang.modular.system.domain.response.common.DictValue;
import pers.dawnyang.modular.system.mapper.SysDictTypeMapper;
import pers.dawnyang.modular.system.mapper.SysDictValueMapper;
import pers.dawnyang.modular.system.service.DictTypeService;

@Service
public class DictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType>
    implements DictTypeService {

  @Autowired
  private SysDictTypeMapper sysDictTypeMapper;

  @Autowired
  private SysDictValueMapper sysDictValueMapper;

  @Autowired
  private DictCacheService dictCacheService;

  // @CreateCache(name = "demoCache.demo",
  // expire = 100, cacheType =
  // CacheType.BOTH, localLimit = 50)
  // private Cache<String, Demo>
  // demoCache;



  @Override
  public void initDictCache() {
    // DictUtils.clearDictCache();
    LambdaQueryWrapper<SysDictType> lqw1 = new LambdaQueryWrapper<>();
    lqw1.eq(SysDictType::getStatus, 1);
    List<SysDictType> dictTypeList = sysDictTypeMapper.selectList(lqw1);
    dictCacheService.setTypeListCache(dictTypeList);
    for (SysDictType sysDictType : dictTypeList) {
      LambdaQueryWrapper<SysDictValue> lqw2 = new LambdaQueryWrapper<>();
      lqw2.eq(SysDictValue::getTypeCode, sysDictType.getTypeCode());
      lqw2.eq(SysDictValue::getStatus, 1);
      List<SysDictValue> dictValueList = sysDictValueMapper.selectList(lqw2);
      List<DictValue> valueList = new ArrayList<>();
      for (SysDictValue sysDictValue : dictValueList) {
        DictValue dictValue = new DictValue();
        BeanUtils.copyProperties(sysDictValue, dictValue);
        valueList.add(dictValue);
      }
      dictCacheService.setValueListCache(sysDictType.getTypeCode(), valueList);
      // System.out.println(sysDictType.getTypeCode());
      // System.out.println(valueList);
      // DictUtils.setDictValueList(sysDictType.getTypeCode(),
      // valueList);
    }

  }

}
